//
//  WBStatusViewModel.swift
//  Weibo
//
//  Created by Albert Zhu on 16/10/19.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

/// 单条微博的视图模型

/*
如果没有任何父类，如果希望在开发时输出调试信息
 1.遵守CustomStringConvertible协议
 2.实现description计算属性
 */

/*
 tableview性能优化
 1.尽量少计算，所有需要的素材提前计算好
 2.空间上不要设置圆角半径，所有图像渲染的属性，都要注意
 3.不要动态创建控件，所有需要的控件，都要提前创建好，在显示的时候，根据数据隐藏/显示
 4.cell中控件的层次越少越好，数量越少越好
 */
class WBStatusViewModel: CustomStringConvertible {
    /// 微博模型
    var status: WBStatus
    /// 会员图标
    var memberIcon: UIImage?
    /// VIP认证图标
    var vipIcon: UIImage?
    /// 转发按钮文字
    var retweetedStr: String?
    /// 正文文字
    var statusAttrText: NSAttributedString?
    /// 被转发微博的文字
    var retweetedAttrText: NSAttributedString?
    /// 评论文字
    var commentStr: String?
    /// 点赞文字
    var likeStr: String?
    /// 姓名颜色
    var nameLabelColor: UIColor?
    /// 配图尺寸
    var pictureSize = CGSize()
    /// 如果是被转发的微博，原创微博一定没有图
    var picURLs: [WBStatusPicture]? {
        // 如果有被转发的微博，返回被转发微博的配图
        // 如果没有被转发的微博，返回原创微博的配图
        // 如果都没有，返回nil
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }

    
    /// 构造函数
    ///
    /// - parameter model: 微博模型
    ///
    /// - returns: 微博视图模型
    init(model: WBStatus) {
        status = model
        
        if (model.user?.mbrank)! > 0 && (model.user?.mbrank)! < 7 {
            let imageName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            
            memberIcon = UIImage(named: imageName)
        }
        
        nameLabelColor = memberIcon == nil ? UIColor.black : UIColor.az_RGBColor(red: 255, green: 108, blue: 0)
        
        switch model.user?.verified_type ?? -1 {
        case 0:
            vipIcon = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            vipIcon = UIImage(named: "avatar_enterprise_vip")
        case 220:
            vipIcon = UIImage(named: "avatar_grassroot")
        default:
            break
        }
        
        retweetedStr = countString(count: model.reposts_count, defaultStr: "转发")
        commentStr = countString(count: model.comments_count, defaultStr: "评论")
        likeStr = countString(count: model.attitudes_count, defaultStr: "赞")
        
        // 计算配图大小(有原创计算原创，有转发计算转发)
        pictureSize = calcPictureViewSize(count: picURLs?.count ?? 0)
        
        // 转发微博的文字格式
        let rText = "@" + (status.retweeted_status?.user?.screen_name ?? "") + ":" + (status.retweeted_status?.text ?? "")
        retweetedAttrText = CZEmotionManager.shared.emotionString(string: rText, font: UIFont.systemFont(ofSize: 15))
        
        // 正文的属性文本
        statusAttrText = CZEmotionManager.shared.emotionString(string: model.text ?? "", font: UIFont.systemFont(ofSize: 17))
}
    
    var description: String {
        return status.description
    }
    
    /// 给定一个数字，返回描述结果
    ///
    /// - parameter count:      数字
    /// - parameter defaultStr: 默认字符串
    ///
    /// - returns: 描述结果
    private func countString(count: Int, defaultStr: String) -> String {
        
        if count == 0 {return defaultStr}
    
        if count < 10000 {return count.description}
        
        return String(format: "%.02f万", Double(count / 10000))
    }
    
    /// 计算指定数量图片对应的配图大小
    ///
    /// - parameter count: 配图数量
    ///
    /// - returns: 配图大小
    private func calcPictureViewSize(count: Int?) -> CGSize {
        
        if count == 0 || count == nil {return CGSize()}
        
        // 计算高度
        let row = (count! - 1) / 3 + 1
        
        var height = WBStatusPictureViewOutterMargin
            height += CGFloat(row) * WBStatusPictureItemWidth + CGFloat(row - 1) * WBStatusPictureViewInnerMargin
        
        return CGSize(width: WBStatusPictureViewWidth, height: height)
    }
    
    /// 使用单个图像更新配图视图的大小
    ///
    /// - parameter image: 网络缓存的单张图像
    func updateSingleImageSize(image: UIImage) {
        var size = image.size
        
        let maxWidth: CGFloat = 240
        let minWidth: CGFloat = 40
        
        if size.width > maxWidth {
            size.height = size.height / size.width * maxWidth
            size.width = maxWidth
        }
        
        if size.width < minWidth {
            size.width = minWidth
            
            size.height = size.width * image.size.height / image.size.width / 4
        }
        
        if size.height > 400 {
            size.height = 400
        }
        
        size.height += WBStatusPictureViewOutterMargin
        
        pictureSize = size
    }
}
